//
//  RJCheckProvinceModel.m
//  AirQuality
//
//  Created by Po on 15/5/5.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCheckProvinceModel.h"

@implementation RJCheckProvinceModel

- (instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendMessage) name:@"sendMessageToCity" object:nil];
    }
    return self;
}

- (void)updateCityStationsCode
{
    //获取所有站点code
    NSMutableDictionary * allCityStations = [NSMutableDictionary dictionary];
    for (NSDictionary * cityData in _checkModel) {
        NSString * cityCode = cityData[@"CITYCODE"];
        NSArray * stations = [cityData objectForKey:@"STATIONTYPES"];
        NSMutableArray * tempStations = [NSMutableArray array];
        for (NSUInteger i = 0; i < stations.count; i ++) {
            if (![RJUserData defaultRJUserData].isProvince) {
                [tempStations addObjectsFromArray:stations];
            } else {
                for (NSDictionary * dic in [stations[i] objectForKey:@"STATIONS"]) {
                    if ([dic[@"STATIONTYPEID"] integerValue] == 4) {
                        //排除直管站
                        continue;
                    }
                    [tempStations addObject:[NSString stringWithFormat:@"'%@'",dic[@"STATIONCODE"]]];
                }
            }
        }
        [allCityStations setObject:tempStations forKey:cityCode];
    }
    _cityStationsCode = [NSDictionary dictionaryWithDictionary:allCityStations];
}

- (void)getCity24ValueWithCityCode:(NSString *)cityCode
{
    _timeArray = [NSMutableArray array];
    if (_cityStationsCode.count == 0) {
        return;
    }
    NSString * time = [RJTools getTimeWithFormat:@"yyyy-MM-dd" time:YesterdayDate];
    NSString * today = [RJTools getTimeWithFormat:@"yyyy-MM-dd" time:[NSDate date]];
    NSDictionary * currentCityDic = [NSDictionary dictionary];
    NSMutableArray * codeArray = [NSMutableArray array];
    if (![RJUserData defaultRJUserData].isProvince) {
        NSUInteger index = 0 ;
        NSArray * keyarr = [_cityStationsCode allKeys];
        for (NSString * key in keyarr) {
            NSString * keyStr = [NSString stringWithFormat:@"%@",key];
            if ([keyStr isEqualToString:cityCode]) {
                index = [keyarr indexOfObject:key];
            }
        }
//        NSDictionary * d = _cityStationsCode[cityCode];
        NSArray * stationsArray = [_cityStationsCode allValues];
        NSArray * dictionary = stationsArray[index];
        NSDictionary * codeArray1 = dictionary[0];
        NSArray * countryStations;
        if (dictionary.count >= 2) {
          NSDictionary * countryCodeArray = dictionary[1];
          countryStations = countryCodeArray[@"STATIONS"];
        }
        NSArray * stations = codeArray1[@"STATIONS"];
        for (NSDictionary * dic in stations) {
            NSString * codeString = dic[@"STATIONCODE"];
            [codeArray addObject:[NSString stringWithFormat:@"'%@'",codeString]];
        }
        if (dictionary.count >= 2) {
            for (NSDictionary * dic in countryStations) {
                [codeArray addObject:[NSString stringWithFormat:@"'%@'",dic[@"STATIONCODE"]]];
            }
        }
      
    }else{
        NSArray * stationsArray = _cityStationsCode[cityCode];
        codeArray = [NSMutableArray arrayWithArray:stationsArray];
    }
    NSString * areaString = [codeArray componentsJoinedByString:@","];
    NSString * url;
    NSUserDefaults * userId = [NSUserDefaults standardUserDefaults];
    NSArray * userIdArr = [userId objectForKey:@"userId1"];
    NSString * userId1 = userIdArr[0];
    NSString * userCityCode = userIdArr[1];
    // @"http://newair.scemc.cn:5100" @"http://xsscd.xicp.net:5100%@"
    NSString * http = @"http://www.scnewair.cn:3389";
    url = [NSString stringWithFormat:@"%@/smartadmin/statAnalyse/getHourDataForApp?type=4&area=%@&starttime=%@%%2001:00:00&endtime=%@%%2000:00:00&gaslist=SO2,NO2,O3,PM10,PM2.5,CO&userId=%@&cityCode=%@",http,areaString,time,today,userId1,userCityCode];
    [RJNetWork myNetRequestWithAddress:url data:@{} completeBlock:^(id responseObject) {
        NSMutableDictionary * city24Value = [NSMutableDictionary dictionary];
        NSMutableDictionary * country24Value = [NSMutableDictionary dictionary];
        
        NSArray * array;             //数据
        NSArray * keyArray;          //关键字
        
        array = [responseObject[@"data"] allValues];
        _all24Arr = [responseObject[@"data"] allValues];
        NSMutableArray * keyArr = [NSMutableArray array];
        for (NSArray * arr in array) {
            NSDictionary * dic = arr[0];
            NSString * key = dic[@"STATIONCODE"] ;
            [keyArr addObject:key];
        }
        NSArray * sortedKeyArr = [keyArr sortedArrayUsingSelector:@selector(compare:)];
        NSMutableArray * sortArr = [NSMutableArray array];
        for (NSString * key in sortedKeyArr) {
            for (NSArray * arr in array) {
                NSDictionary * dic = arr[0];
                NSString * stationKey = dic[@"STATIONCODE"] ;
                if ([stationKey isEqualToString:key]) {
                    [sortArr addObject:arr];
                }
            }
        }
        array = [NSArray arrayWithArray:sortArr];
        keyArray = @[@"AQI",@"CO",@"NO2",@"O3",@"PM10",@"PM2.5",@"SO2"];
        if (array.count != 0) {
            for (NSDictionary * dic in array[0]) {
                NSString *timeString = dic[@"TIMEPOINT"];
                [_timeArray addObject:[timeString substringWithRange:NSMakeRange(11, 2)]];
            }
        }
        for (NSString * key in keyArray) {
            NSMutableArray * typeArray = [NSMutableArray array];
            NSMutableArray * typeArray1 = [NSMutableArray array];
            NSMutableArray * stationNames = [NSMutableArray array];
            NSMutableArray * countryStationNames = [NSMutableArray array];
            for (NSArray * timeArray in array) {
                //分情况获取数据
                NSMutableArray * tempData = [NSMutableArray array];
                for (NSDictionary * dic in timeArray) {
                    NSString * numString =dic[key];
                    [tempData addObject:numString];
                }
                NSString * stationCode = timeArray[0][@"STATIONCODE"];
                if ([stationCode rangeOfString:@"A"].location != NSNotFound) {
                    [typeArray addObject:@{stationCode:tempData}];
                    //获取站点名
                    NSString * stationName = timeArray[0][@"STATIONNAME"];
                    NSRange positionRange = [stationName rangeOfString:@"_"];
                    [stationNames addObject:[stationName substringFromIndex:positionRange.location + 1]];
                }else{
                    [typeArray1 addObject:@{stationCode:tempData}];
                    NSString * stationName = timeArray[0][@"STATIONNAME"];
                    NSRange positionRange = [stationName rangeOfString:@"_"];
                    [countryStationNames addObject:[stationName substringFromIndex:positionRange.location + 1]];
 
                }
            }
            [country24Value setObject:typeArray1 forKeyedSubscript:key];
            [city24Value setObject:typeArray forKey:key];
          
            if ([keyArray indexOfObject:key] == 0) {
                _cityStationsName = [NSArray arrayWithArray:stationNames];
                _countryStationsName = [NSArray arrayWithArray:countryStationNames];
            }
        }
        _country24Dic = [NSDictionary dictionaryWithDictionary:country24Value];
        _city24Dic = [NSDictionary dictionaryWithDictionary:city24Value];
        NSDictionary * userInfo = @{@"result":@"1"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"checkDidGetCity24Value" object:userInfo];
    }failBlack:^(id response) {
        NSDictionary * userInfo = @{@"result":@"0"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"checkDidGetCity24Value" object:userInfo];
    }];
}

- (void)getAll24Arr
{
    NSMutableDictionary * city24Value = [NSMutableDictionary dictionary];
    NSMutableDictionary * country24Value = [NSMutableDictionary dictionary];
    NSArray * array;             //数据
    NSArray * keyArray;          //关键字
    array = _all24Arr;
    keyArray = @[@"AQI",@"CO",@"NO2",@"O3",@"PM10",@"PM2.5",@"SO2"];
    if (array.count != 0) {
        for (NSDictionary * dic in array[0]) {
            NSString *timeString = dic[@"TIMEPOINT"];
            [_timeArray addObject:[timeString substringWithRange:NSMakeRange(11, 2)]];
            
        }
    }
    
    for (NSString * key in keyArray) {
        NSMutableArray * typeArray = [NSMutableArray array];
        NSMutableArray * typeArray1 = [NSMutableArray array];
        NSMutableArray * stationNames = [NSMutableArray array];
        NSMutableArray * countryStationNames = [NSMutableArray array];
        
        for (NSArray * timeArray in array) {
            //分情况获取数据
            NSMutableArray * tempData = [NSMutableArray array];
            for (NSDictionary * dic in timeArray) {
                NSString * numString =dic[key];
                [tempData addObject:numString];
            }
            NSString * stationCode = timeArray[0][@"STATIONCODE"];
            if ([stationCode rangeOfString:@"A"].location != NSNotFound) {
                [typeArray addObject:@{stationCode:tempData}];
                //获取站点名
                NSString * stationName = timeArray[0][@"STATIONNAME"];
                NSRange positionRange = [stationName rangeOfString:@"_"];
                [stationNames addObject:[stationName substringFromIndex:positionRange.location + 1]];
            }else{
                [typeArray1 addObject:@{stationCode:tempData}];
                NSString * stationName = timeArray[0][@"STATIONNAME"];
                NSRange positionRange = [stationName rangeOfString:@"_"];
                [countryStationNames addObject:[stationName substringFromIndex:positionRange.location + 1]];
                
            }
        }
        [country24Value setObject:typeArray1 forKeyedSubscript:key];
        [city24Value setObject:typeArray forKey:key];
        
        if ([keyArray indexOfObject:key] == 0) {
            _cityStationsName = [NSArray arrayWithArray:stationNames];
            _countryStationsName = [NSArray arrayWithArray:countryStationNames];
        }
    }
    _country24Dic = [NSDictionary dictionaryWithDictionary:country24Value];
    _city24Dic = [NSDictionary dictionaryWithDictionary:city24Value];
}

@end
